package com.ruangong.GitClient.app.Password;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.ruangong.GitClient.R;

public class AddPasswordActivity extends AppCompatActivity {

    private EditText websiteNameEditText, usernameEditText, passwordEditText;
    private Button add;
    private PasswordDBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addpassword);

        websiteNameEditText = findViewById(R.id.websiteNameEditText);
        usernameEditText = findViewById(R.id.usernameEditText);
        passwordEditText = findViewById(R.id.passwordEditText);
        add = findViewById(R.id.add);
        dbHelper = new PasswordDBHelper(this);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取用户输入的密码信息
                String websiteName = websiteNameEditText.getText().toString().trim();
                String username = usernameEditText.getText().toString().trim();
                String password = passwordEditText.getText().toString().trim();

                // 进行简单的验证
                if (websiteName.isEmpty() || username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(AddPasswordActivity.this, "Please fill in all fields", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 将密码信息保存到数据库
                try {
                    addPasswordToDatabase(username, password, websiteName);
                    // 清空输入框
                    websiteNameEditText.setText("");
                    usernameEditText.setText("");
                    passwordEditText.setText("");
                    // 显示添加成功消息
                    Toast.makeText(AddPasswordActivity.this, "Password added successfully", Toast.LENGTH_SHORT).show();
                    // 可选：你也可以在添加密码后返回到主界面
                    finish();
                } catch (Exception e) {
                    Toast.makeText(AddPasswordActivity.this, "Failed to add password: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            private void addPasswordToDatabase(String username, String password, String websiteName) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                try {
                    db = dbHelper.getWritableDatabase();

                    ContentValues values = new ContentValues();
                    values.put(PasswordDBHelper.COLUMN_USERNAME, username);
                    values.put(PasswordDBHelper.COLUMN_PASSWORD, password);
                    values.put(PasswordDBHelper.COLUMN_WEBSITE, websiteName);

                    long newRowId = db.insert(PasswordDBHelper.TABLE_NAME, null, values);
                    if (newRowId == -1) {
                        throw new RuntimeException("Failed to insert data into the database.");
                    }
                } catch (Exception e) {
                    // 处理异常，比如记录日志或者提示用户保存失败
                    e.printStackTrace();
                } finally {
                    // 关闭数据库连接
                    if (db != null) {
                        db.close();
                    }
                }
            }
        });
    }
}